package com.tsx.ai.controller;

import com.tsx.ai.service.ChatService;
import lombok.RequiredArgsConstructor;
import org.springframework.ai.document.Document;
import org.springframework.ai.vectorstore.SearchRequest;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;

import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/chat")
public class ChatController {

    private final ChatService chatService;
    private final VectorStore vectorStore;

    @PostMapping("/{sessionId}")
    public String chat(@PathVariable String sessionId, @RequestBody String question) {
        return chatService.chat(sessionId, question);
    }

    @PostMapping(value = "/stream/{sessionId}", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<String> chatStream(@PathVariable String sessionId, @RequestBody String question) {
        return chatService.chatStream(sessionId, question);
    }

    /**
     * 搜索向量数据库
     *
     * @param query 查询条件
     */
    @PostMapping("/search")
    public List<Document> search(@RequestParam("query") String query) {
        return vectorStore.similaritySearch(
                SearchRequest.builder()
                        .query(query) // 设置查询条件
                        .topK(3) // 设置最多返回的文档数量
                        .build()
        );
    }
    /*
    [
    {
        "id": "5f6d700e-de82-4770-ad7d-0a30d2561cd6",
        "text": "北京:101010100 朝阳:101010300 顺义:101010400 怀柔:101010500 通州:101010600 昌平:101010700 延庆:101010800 丰台:101010900 石景山:101011000 大兴:101011100 房山:101011200 密云:101011300 门头沟:101011400 平谷:101011500 八达岭:101011600 佛爷顶:101011700 汤河口:101011800 密云上甸子:101011900 斋堂:101012000 霞云岭:101012100 海淀:101010200 天津:101030100",
        "media": null,
        "metadata": {
            "charset": "UTF-8",
            "filename": "citys.txt",
            "source": "citys.txt",
            "distance": 0.3423191684915431
        },
        "score": 0.6576808315084569
    },
    {
        "id": "4cfe8e80-419a-41df-acbc-3cd476334d7f",
        "text": "哈密:101131201 和田:101131301 阿勒泰:101131401 阿图什:101131501 博乐:101131601 克拉玛依:101130201 拉萨:101140101 山南:101140301 阿里:101140701 昌都:101140501 那曲:101140601 日喀则:101140201 林芝:101140401 台北县:101340101 高雄:101340201 台中:101340401 海口:101310101 三亚:101310201 东方:101310202 临高:101310203 澄迈:101310204 儋州:101310205 昌江:101310206",
        "media": null,
        "metadata": {
            "charset": "UTF-8",
            "filename": "citys.txt",
            "source": "citys.txt",
            "distance": 0.423554191165626
        },
        "score": 0.576445808834374
    },
    {
        "id": "d7156705-c793-454d-bcec-c410a41270a8",
        "text": "金山:101020700 石家庄:101090101 张家口:101090301 承德:101090402 唐山:101090501 秦皇岛:101091101 沧州:101090701 衡水:101090801 邢台:101090901 邯郸:101091001 保定:101090201 廊坊:101090601 郑州:101180101 新乡:101180301 许昌:101180401 平顶山:101180501 信阳:101180601 南阳:101180701 开封:101180801 洛阳:101180901 商丘:101181001 焦作:101181101 鹤壁:101181201",
        "media": null,
        "metadata": {
            "charset": "UTF-8",
            "filename": "citys.txt",
            "source": "citys.txt",
            "distance": 0.42361547590670645
        },
        "score": 0.5763845240932935
    }
]
     */
}
